﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace MicroTest
{
    /// <summary>
    /// 学生标签
    /// </summary>
    /// [Serializable]
    [Serializable]
    class StudentCard
    {
        public string Name { set; get; }
        public string PageId { set; get; }
        public int LeftBound { set; get; }
        public int TopBound { set; get; }
        public int CardWidth { set; get; }
        public int CardHeight { set; get; }
        public string StudentID { set; get; }
    }
    [Serializable]
    class MicroTestInfo
    {
        /// <summary>
        /// 微测ID
        /// </summary>
        public string MicroTestID { get; set; }
        /// <summary>
        /// 考试名
        /// </summary>
        public string MicroTestTitle { get; set; }
        /// <summary>
        /// 考场班级信息
        /// </summary>
        public string MicroTestRoomInfo { get; set; }
        /// <summary>
        /// 标签卡列表
        /// </summary>
        public List<StudentCard> CardList { get; set; }
        /// <summary>
        /// 考试时间
        /// </summary>
        public DateTime MicroTestDate {get;set;}
        /// <summary>
        /// 试卷地址列表
        /// </summary>
        public List<string> MicroTestPaperPageSerial { get; set; }
        /// <summary>
        /// 学生标签地址列表
        /// </summary>
        public List<string> MicroTestStudentCardPageSerial { get; set; }
        /// <summary>
        /// 试卷背景
        /// </summary>
        public Dictionary<string,Image> MicroTestPaperImage { get; set; }
        /// <summary>
        /// 考试数据存储路径
        /// </summary>
        public string MicroTestDataFolder { get; private set; }
        public string MicroTestGradeID { get; set; }
        public string MicroTestClassID { get; set; }
        public string MicroTestLianxiceID { get; set; }
        public string MicroTestZhangjieID { get; set; }
        public string MicroTestUserName { get; set; }

        /// <summary>
        /// 添加试卷背景图片
        /// </summary>
        /// <param name="pageSerial">图片地址</param>
        /// <param name="img">图片</param>
        public void AddPaperPageImage(string pageSerial,Image img)
        {
            if (MicroTestPaperImage == null)
                MicroTestPaperImage = new Dictionary<string, Image>();
            if(MicroTestPaperImage.ContainsKey(pageSerial)==false)
                MicroTestPaperImage.Add(pageSerial, img);
            AddPaperPageSerial(pageSerial);
        }
        /// <summary>
        /// 添加试卷图片地址到图片地址列表
        /// </summary>
        /// <param name="pageSerial">图片地址</param>
        private void AddPaperPageSerial(string pageSerial)
        {
            if (MicroTestPaperPageSerial == null)
                MicroTestPaperPageSerial = new List<string>();
            foreach (var item in MicroTestPaperPageSerial)
            {
                if (item.Equals(pageSerial))
                    return;
            }
            MicroTestPaperPageSerial.Add(pageSerial);
        }
        /// <summary>
        /// 清除图片
        /// </summary>
        public void ClearPaperPageImage()
        {
            if (MicroTestPaperImage != null)
                MicroTestPaperImage.Clear();
            if (MicroTestPaperPageSerial != null)
                MicroTestPaperPageSerial.Clear();
        }
        /// <summary>
        /// 通过学生姓名返回学生卡片信息
        /// </summary>
        /// <param name="studentName"></param>
        /// <returns></returns>
        public StudentCard GetStudentCardByName(string studentName)
        {
            foreach (var item in CardList)
            {
                if (studentName.Equals(item.Name))
                    return item;
            }
            return null;
        }
        /// <summary>
        /// 添加学生签到卡信息
        /// </summary>
        /// <param name="card">学生签到卡</param>
        public void AddStudentCard(StudentCard card)
        {
            if (CardList == null)
                CardList = new List<StudentCard>();
            CardList.Add(card);
            if (MicroTestStudentCardPageSerial == null)
                MicroTestStudentCardPageSerial = new List<string>();
            for (int i = 0; i < MicroTestStudentCardPageSerial.Count; i++)
            {
                if (MicroTestStudentCardPageSerial[i].Equals(card.PageId))
                    return;
            }
            MicroTestStudentCardPageSerial.Add(card.PageId);
        }
        /// <summary>
        /// 清除标签信息
        /// </summary>
        public void ClearStudentCard()
        {
            if (CardList != null)
                CardList.Clear();
            if (MicroTestStudentCardPageSerial != null)
                MicroTestStudentCardPageSerial.Clear();
        }
        /// <summary>
        /// 获取学生信息
        /// </summary>
        /// <param name="pageID"></param>
        /// <param name="nx"></param>
        /// <param name="ny"></param>
        /// <returns></returns>
        public string GetStudentName(string pageID,int nx,int ny)
        {
            string name = string.Empty;
            if (CardList == null)
            {
                ELogger.Error("学生列表信息为空");
                return name;
            }
                
            foreach (var item in CardList)
            {
                if(item.PageId.Equals(pageID)&&nx>=item.LeftBound&&nx<=item.LeftBound+item.CardWidth
                    &&ny>=item.TopBound&&ny<=item.TopBound+item.CardHeight)
                {
                    name = item.Name;
                    break;
                }
            }
            return name;
        }
        /// <summary>
        /// 判断当前地址是否为学生签到卡地址
        /// </summary>
        /// <param name="pageSerial"></param>
        /// <returns></returns>
        public bool IsStudentCardPage(string pageSerial)
        {
            if (MicroTestStudentCardPageSerial == null)
                return false;
            bool ret = false;
            for (int i = 0; i < MicroTestStudentCardPageSerial.Count; i++)
            {
                if(MicroTestStudentCardPageSerial[i].Equals(pageSerial))
                {
                    ret = true;
                    break;
                }
            }
            return ret;
        }
        /// <summary>
        /// 判断当前地址是否为试卷地址
        /// </summary>
        /// <param name="pageSerial"></param>
        /// <returns></returns>
        public bool IsPaperPageSerial(string pageSerial)
        {
            if (MicroTestPaperPageSerial == null)
                return false;
            bool ret = false;
            for (int i = 0; i < MicroTestPaperPageSerial.Count; i++)
            {
                if (MicroTestPaperPageSerial[i].Equals(pageSerial))
                {
                    ret = true;
                    break;
                }
            }
            return ret;
        }
        public bool IniExam()
        {
            //Program.TEST_KEY = fs.TestKey;
            string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\WiseClass";

            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);
            Program.MTInfo.MicroTestDate = DateTime.Now;
            string dateStr = DateTime.Now.ToString("yyyy-MM-dd");
            folder = folder + "\\" + dateStr;
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);
            Program.DATA_ROOT = folder;
            folder = folder + "\\" + MicroTestID;
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);
            Program.DATA_FOLDER = folder;
            MicroTestDataFolder = folder;
            ELogger.Trace(string.Format("微测密钥:{0}", Program.TEST_KEY));
            ELogger.Trace(string.Format("微测数据存储路径:{0}", Program.DATA_FOLDER));
            return true;
        }

    }
}
